package main

import (
	"example.com/m/baidu"
	"fmt"
	"github.com/go-vgo/robotgo"
	"log"
	"strings"
	"time"
)

func main() {
	var loc baidu.Locations
	var backup_loc baidu.Locations
	//进行截图
	filename := Begin()
	if filename == "" {
		return
	}
	//ocr识别
	data := baidu.AccurateBasic("zawu/lalaimg.png")
	for _, v := range data.WordsResult {
		if find := strings.Contains(v.Words, "开始游戏"); find {
			loc = v.Location
		}
	}
	log.Println(loc)
	// 左键
	robotgo.MoveClick(int(loc.Left), int(loc.Top), "left", true)
	fmt.Println(robotgo.GetMousePos())

	time.Sleep(3 * time.Second)

	filename1 := Begin()
	if filename1 == "" {
		return
	}
	//ocr识别
	data1 := baidu.AccurateBasic("zawu/lalaimg.png")
	for _, v := range data1.WordsResult {
		if find := strings.Contains(v.Words, "退出"); find {
			backup_loc = v.Location
		}
	}
	if backup_loc.Height == 0 {
		return
	}
	log.Println("backup loc is :", backup_loc)
	robotgo.MoveClick(int(backup_loc.Left), int(backup_loc.Top), "left", true)
	log.Println(" over")
}

func Begin() (filename string) {
	bitmap := robotgo.CaptureScreen()
	defer robotgo.FreeBitmap(bitmap)
	img := robotgo.ToImage(bitmap)
	err := robotgo.Save(img, fmt.Sprintf("zawu/lalaimg.png"))
	if err != nil {
		fmt.Println("screen is ok")
		return ""
	}
	return "lalaimg.png"
}
